﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Fashion_API;

namespace Fashion.BackEnd.ManageAccount
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            using (DALDataContext d = new DALDataContext())
            {
                grdAccount.DataSource = d.Accounts;
                grdAccount.DataBind();
            }
        }

        private void BindDataToCboUserRole(DropDownList cbo)
        {
            using (DALDataContext d = new DALDataContext())
            {
                cbo.DataSource = d.UserRoles;
                cbo.DataTextField = "Name";
                cbo.DataValueField = "Id";
                cbo.DataBind();
            }
        }

        protected void grdAccount_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DropDownList cboUserRole = (DropDownList) e.Item.FindControl("cboUserRole");
                BindDataToCboUserRole(cboUserRole);
                if (DataBinder.Eval(e.Item.DataItem, "UserRoleId") != null)
                    cboUserRole.SelectedValue = DataBinder.Eval(e.Item.DataItem, "UserRoleId").ToString();
            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
                DropDownList cboUserRole = (DropDownList) e.Item.FindControl("cboUserRole");
                BindDataToCboUserRole(cboUserRole);
            }
        }

        protected void grdAccount_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "cmdAddNew":
                    using(DALDataContext d = new DALDataContext())
                    {
                        Account account = new Account();
                        DropDownList cboUserRole = (DropDownList) e.Item.FindControl("cboUserRole");
                        TextBox txtUserName = (TextBox) e.Item.FindControl("txtUserName");
                        TextBox txtPassword = (TextBox) e.Item.FindControl("txtPassword");
                        TextBox txtFirstName = (TextBox) e.Item.FindControl("txtFirstName");
                        TextBox txtLastName = (TextBox) e.Item.FindControl("txtLastName");
                        TextBox txtEmail = (TextBox)e.Item.FindControl("txtEmail");
                        account.UserRoleId = Convert.ToInt32(cboUserRole.SelectedValue);
                        account.UserName = txtUserName.Text.Trim();
                        account.Password = txtPassword.Text.Trim();
                        account.FirstName = txtFirstName.Text.Trim();
                        account.LastName = txtLastName.Text.Trim();
                        account.LastName = txtLastName.Text.Trim();
                        account.Email = txtEmail.Text.Trim();
                        account.DateCreate = DateTime.Now;
                        account.Money = 0;
                        account.IsActive = true;
                        d.Accounts.InsertOnSubmit(account);
                        d.SubmitChanges();
                    }
                    break;
                case "cmdUpdate":
                    using(DALDataContext d = new DALDataContext())
                    {
                        Account account = d.Accounts.SingleOrDefault(i => i.Id == Convert.ToInt32(e.CommandArgument));
                        DropDownList cboUserRole = (DropDownList) e.Item.FindControl("cboUserRole");
                        TextBox txtUserName = (TextBox) e.Item.FindControl("txtUserName");
                        TextBox txtPassword = (TextBox) e.Item.FindControl("txtPassword");
                        TextBox txtFirstName = (TextBox) e.Item.FindControl("txtFirstName");
                        TextBox txtLastName = (TextBox) e.Item.FindControl("txtLastName");
                        TextBox txtEmail = (TextBox)e.Item.FindControl("txtEmail");
                        account.UserRoleId = Convert.ToInt32(cboUserRole.SelectedValue);
                        account.UserName = txtUserName.Text.Trim();
                        account.Password = txtPassword.Text.Trim();
                        account.FirstName = txtFirstName.Text.Trim();
                        account.LastName = txtLastName.Text.Trim();
                        account.LastName = txtLastName.Text.Trim();
                        account.Email = txtEmail.Text.Trim();
                        d.SubmitChanges();
                    }
                    break;
                case "cmdDelete":
                    using(DALDataContext d = new DALDataContext())
                    {
                        Account account = d.Accounts.SingleOrDefault(i => i.Id == Convert.ToInt32(e.CommandArgument));
                        d.Accounts.DeleteOnSubmit(account);
                        d.SubmitChanges();
                    }
                    break;
                case "cmdIsActive":
                    using(DALDataContext d = new DALDataContext())
                    {
                        Account account = d.Accounts.SingleOrDefault(i => i.Id == Convert.ToInt32(e.CommandArgument));
                        account.IsActive = !account.IsActive;
                        d.SubmitChanges();
                    }
                    break;
            }
            BindData();
        }
    }
}